<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>登录</title>
  <link rel="stylesheet" href="./css/login.css" />
</head>

<body>
  <!DOCTYPE html>
  <html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="./js/login.js">
  </head>

  <body>
    <div id="all">
      <div class="content">
        <div class="left">
          <img src="./image/保尔科技-logo-副本.png" alt="" />
        </div>
        <div class="right">
          <div class="cont">
            <p>保尔科技办公自动化平台</p>
            <input class="inp1" type="text" placeholder="用户账号" />
            <input class="inp2" type="password" placeholder="用户密码" />
            <button>登录</button>
          </div>
        </div>
      </div>
    </div>
  </body>
  <script src="./js/connector.js"></script>
  <script>
    var inp1 = document.getElementsByClassName('inp1')[0];
    var inp2 = document.getElementsByClassName('inp2')[0];
    function user() {
      var str = /^[a-z][a-z0-9]{3,15}/ig;
      var flage = true;
      if (inp1.value == "") {
        alert('请输入用户名');
        flage = false
        return flage;
      } else if (str.test(inp1.value) != true) {
        alert('用户名输入不正确');
        flage = false
        return flage;
      } else {
        return flage;
      }

    }
    function mypassword() {
      var str1 = /[a-z0-9]{4,10}/ig;
      var flage = true;
      if (inp2.value == "") {
        alert('请输入密码');
        flage = false;
        return flage;
      } else if (str1.test(inp2.value) != true) {
        alert('密码输入不正确');
        flage = false;
        return flage;
      } else {
        return flage;
      }
    }
    var btn = document.querySelector('button');
    btn.onclick = function () {
      if (user() && mypassword()) {
        var xhr = new XMLHttpRequest()
        xhr.open("post", "http://192.168.199.101:8080/BaorSystem/user/login")
        xhr.setRequestHeader("content-type", "application/json");
        var daet = {
          userAccount: inp1.value,
          userPassword: inp2.value
        }
        xhr.send(JSON.stringify(daet));
        xhr.onreadystatechange = function () {
          if (xhr.readyState == 4 && xhr.status == 200) {
            var data = JSON.parse(xhr.responseText);
            console.log(data);
            if (data.code == 200) {
              alert('登陆成功');
              localStorage.setItem("userName", inp1.value);
              localStorage.setItem("userName", inp2.value);
              localStorage.setItem("userId", data.dataset.userInfo.userId);
              location.href = 'header.html';
            }
          }
        }

      }
    }

  </script>

  </html>
</body>


</html>